package tu.appengine.servlet.pdf;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.blobstore.BlobKey;
import com.google.appengine.api.blobstore.BlobstoreService;
import com.google.appengine.api.blobstore.BlobstoreServiceFactory;
import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

public class BlobstoreAccess extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();

		String potok = req.getParameter("potok");
		Key key = KeyFactory.createKey("Program", potok);
		DatastoreService datastoreService = DatastoreServiceFactory.getDatastoreService();
		Entity entity = null;
		try {
			entity = datastoreService.get(key);
			BlobKey blobKey = (BlobKey)entity.getProperty("blobKey");
			blobstoreService.serve(blobKey, resp);
		} catch (EntityNotFoundException e) {
			e.printStackTrace();
		}
	}
}
